package com.code.day16.c_thread;

/*
* 同步方法实现生产者消费者问题
* */

public class BaoZiPu {
    private int count;
    private boolean flag;

    public BaoZiPu(int count, boolean flag) {
        this.count = count;
        this.flag = flag;
    }

    public BaoZiPu() {
    }


    public synchronized void getCount() {
        while (!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费了第" + count+ "个包子");
        this.flag = false;
        this.notifyAll();
    }

    public synchronized void setCount() {
        while (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count ++;
        System.out.println("包子铺正在做第" + count + "个包子");
        this.flag = true;
        this.notifyAll();
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
